package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.util.NumberUtils;
import com.cskaoyan.mall.commons.util.ObjectEmptyUtils;
import com.cskaoyan.order.biz.callback.SendEmailCallback;
import com.cskaoyan.order.biz.callback.TransCallback;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import com.cskaoyan.order.utils.UserNickNameUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化订单 生成订单
 * 向订单表中插入一条订单数据
 * 向订单商品关联表中插入N条数据
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    static final String ORDERKEYNAME = "orderKeyName";
    static final String ORDERITEMKEYNAME = "orderItemKeyName";

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        createOrderContext.setBuyerNickName(UserNickNameUtils.get(createOrderContext.getUserId()));

        Order order = new Order();
        String orderId = globalIdGeneratorUtil.getNextSeq(ORDERKEYNAME, 1);
        order.setOrderId(orderId);
        order.setPayment(createOrderContext.getOrderTotal());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        order.setUserId(createOrderContext.getUserId());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setBuyerNick(createOrderContext.getBuyerNickName());

        int rows = orderMapper.insert(order);

        if (rows < 1) {
            throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION.getCode(), OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
        }

        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            String id = globalIdGeneratorUtil.getNextSeq(ORDERITEMKEYNAME, 2);
            orderItem.setId(id);
            orderItem.setOrderId(orderId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            BigDecimal total = cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(total.doubleValue());
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setStatus(1);

            int rows2 = orderItemMapper.insert(orderItem);

            if (rows2 < 1) {
                throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION.getCode(), OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
            }

        }
        createOrderContext.setOrderId(orderId);
        return true;
    }

}
